﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.DirectoryServices;
using System.Data;

/// <summary>
/// Summary description for LDAPHelp
/// </summary>
public class LDAPHelp
{
    string mysql, name;
    string adminUser = WebConfigurationManager.AppSettings["adAdminUser"];
    string adminPassword = WebConfigurationManager.AppSettings["adAdminPassword"];
    string _path = "LDAP://10.11.20.243/OU=Accounts,DC=avila,DC=edu";
    DataSet ds;

	public LDAPHelp()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public string CurrentUsersADInfo(string user)
    {
        System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry(_path, adminUser, adminPassword);
        System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);
        mySearcher.Filter = "(&(objectClass=user)(SAMAccountName=ramaswamymv))";
        mySearcher.SearchScope = SearchScope.Subtree;
        SearchResult result = mySearcher.FindOne();
        if (!(result == null))
        {
            //System.DirectoryServices.DirectoryEntry de = new System.DirectoryServices.DirectoryEntry(_path, adminUser, adminPassword);
            name = result.Properties["HomeDirectory"][0].ToString();

        }
        return name;
    }
    public DataSet getDetailsBy(string user)
    {
        System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry(_path, adminUser, adminPassword);
        System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);
        mySearcher.Filter = "(&(objectClass=user)(SAMAccountName=" + user + "))";
        //SortOption option = new SortOption("Mail", System.DirectoryServices.SortDirection.Ascending);
        //mySearcher.Sort = option;
        mySearcher.SearchScope = SearchScope.Subtree;
        SearchResult result = mySearcher.FindOne();
        ds = new DataSet();
        DataTable dtUsers = new DataTable("UsersDetails");
        dtUsers.Columns.Add("userid");
        dtUsers.Columns.Add("Name");
        dtUsers.Columns.Add("FirstName");
        dtUsers.Columns.Add("LastName");
        dtUsers.Columns.Add("Phone");
        dtUsers.Columns.Add("email");
        dtUsers.Columns.Add("website");
        dtUsers.Columns.Add("title");
        dtUsers.Columns.Add("department");
        dtUsers.Constraints.Add("UniqueUserId", dtUsers.Columns["userid"], true);






        if (!(result == null))
        {
            System.DirectoryServices.DirectoryEntry de = new System.DirectoryServices.DirectoryEntry(_path, adminUser, adminPassword);

            DataRow drUser = dtUsers.NewRow();
          
            drUser["userid"] = user;
            drUser["Name"] = result.Properties["displayName"][0].ToString(); //de.Properties["name"].Value.ToString(); //de.Properties["sn"][0].ToString() + ", " + de.Properties["givenName"][0].ToString(); 
            drUser["FirstName"] = result.Properties["givenName"][0].ToString();
            drUser["LastName"] = result.Properties["sn"][0].ToString();
            drUser["email"] = result.Properties["mail"][0].ToString();
            drUser["Phone"] = result.Properties["telephoneNumber"][0].ToString();
            drUser["Title"] = result.Properties["title"][0].ToString();
            drUser["department"] = result.Properties["department"][0].ToString();
            dtUsers.Rows.Add(drUser);

           




        }
        ds.Tables.Add(dtUsers);
        return ds;

    }
}
